package com.victor.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 常用公共类
 */
public class CommUtils {

    public static String URL_PREFIX = "/mina/download/";

    public static String DEFAULT_IMGPATH = "/mina/download/not.jpg";

    public static String DEFAULT_IMGNAME = "not.jpg";


    public static boolean isEmpty(String str) {
        return str == null || str.length() < 1;
    }

    public static boolean notEmpty(String str) {
        return str != null && str.length() > 0;
    }

    public static Integer toInteger(String str) {
        return Integer.valueOf(str);
    }

    /**
     * 获得随机字符串（数字加小写字母）
     *
     * @param length
     * @return
     */
    public static String randomNumAndLowerCaseLetters(int length) {
        char[] codeSequence = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        StringBuffer randomStr = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
            randomStr.append(strRand);
        }
        return randomStr.toString();
    }

    /**
     * 获得随机字符串（数字加大写字母）
     *
     * @param length
     * @return
     */
    public static String randomNumAndUpperCaseLetters(int length) {
        char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        StringBuffer randomStr = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
            randomStr.append(strRand);
        }
        return randomStr.toString();
    }

    /**
     * 获得随机字符串（纯数字）
     *
     * @param length
     * @return
     */
    public static String randomNum(int length) {
        char[] codeSequence = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        StringBuffer randomStr = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
            randomStr.append(strRand);
        }
        return randomStr.toString();
    }

    /**
     * 获得随机字符串（纯字母）
     *
     * @param length
     * @return
     */
    public static String randomLetters(int length) {
        char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer randomStr = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
            randomStr.append(strRand);
        }
        return randomStr.toString();
    }

    /**
     * 将日期date按照formater格式转换为字符串
     *
     * @param date
     * @param formater
     * @return 若date为null则返回空串，其他情况下的转换失败则抛出相应异常
     * @author
     */
    public static String formatDateToString(Date date, String formater) {
        if (date == null) {
            return "";
        } else {
            return new SimpleDateFormat(formater).format(date);
        }
    }

    /**
     * 将整型封装类integer转换为字符串
     *
     * @param integer
     * @return 若integer为null则返回空串，其他情况下的转换失败则抛出相应异常
     * @author
     */
    public static String formatIntegerToString(Integer integer) {
        if (integer == null) {
            return "";
        } else {

            return integer.toString();
        }
    }

    public static String formatNullToString(String str) {
        if (str == null) {

            return "";
        } else {

            return "";
        }
    }

    public static String getProperties(String pro, InputStream in) {
        Properties pop = new Properties();
        try {
            pop.load(in);
            String value = pop.getProperty(pro);
            return value;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将BigDecimal类型按指定的格式format格式化
     *
     * @param num
     * @param format
     * @return
     */
    public static String format(BigDecimal num, String format) {
        DecimalFormat df = new DecimalFormat(format);
        if (num != null) {
            return df.format(num.doubleValue());
        } else {
            return df.format(0);
        }
    }

    /**
     * 根据身份证号码算性别
     *
     * @param idNo
     * @return
     */
    public static String getSexFromIdNo(String idNo) {
        String commConfigSexName = "";
        String sex = idNo.substring(idNo.length() - 2, idNo.length() - 1);
        if (Integer.parseInt(sex) % 2 == 0) {
            commConfigSexName = "女";
        } else {
            commConfigSexName = "男";
        }
        return commConfigSexName;
    }

    /**
     * 从身份证获取出生日期
     *
     * @param cardNumber 已经校验合法的身份证号
     * @return Strig YYYY-MM-DD 出生日期
     */
    public static String getBirthDateFromCard(String cardNumber) {
        String card = cardNumber.trim();
        String year;
        String month;
        String day;
        /**
         *  处理18位身份证
         */
        if (card.length() == 18) {
            year = card.substring(6, 10);
            month = card.substring(10, 12);
            day = card.substring(12, 14);
        } else { // 处理非18位身份证
            year = card.substring(6, 8);
            month = card.substring(8, 10);
            day = card.substring(10, 12);
            year = "19" + year;
        }
        if (month.length() == 1) {
            /**
             * 补足两位
             */
            month = "0" + month;
        }
        if (day.length() == 1) {
            /**
             * 补足两位
             */
            day = "0" + day;
        }
        return year + "-" + month + "-" + day;
    }

    /**
     * 小数去0
     *
     * @param str
     * @return
     * @author Jiabin.Zhao
     * @date 创建时间：2016年8月31日 下午4:51:58
     * @version 1.0
     * @parameter
     * @since
     */
    public static String trim(String str) {
        if (str.indexOf(".") != -1 && str.charAt(str.length() - 1) == '0') {
            return trim(str.substring(0, str.length() - 1));
        } else {
            return str.charAt(str.length() - 1) == '.' ? str.substring(0, str.length() - 1) : str;
        }
    }

    private static Map<String, Object> errMap = new HashMap<>();

    /**
     * 上传错误信息map
     *
     * @param message
     * @return
     */
    public static Map<String, Object> getErrMap(String message) {
        if (errMap == null) {
            errMap = new HashMap<>(2);
        }
        errMap.clear();
        errMap.put("success", false);
        errMap.put("message", message);
        return errMap;
    }

    /**
     * <b>Description:生成唯一id</b><br>
     *
     * @param prefix
     * @param digit
     * @param val
     * @return
     * @Note <b>Author:</b>zhaojiabin</a>
     * <br><b>Date:</b> 2017年10月30日 下午5:48:43
     * <br><b>Version:</b> 1.0
     */
    public static String generateOnlyId(String prefix, int digit, int val) {
        StringBuffer sb = new StringBuffer(String.valueOf(val));
        int len = digit - sb.length();
        for (int i = 0; i < len; i++) {
            sb.insert(0, "0");
        }
        sb.insert(0, prefix);
        return sb.toString();
    }

    /**
     * 创建预登记号
     *
     * @param count  当前预登记号
     * @param code   字符串起始头
     * @param length 长度
     * @return
     * @author Jiabin.Zhao
     * @date 创建时间：2016年12月1日 上午10:58:46
     * @version 1.0
     * @parameter
     * @since
     */
    public static String createCustomerCode(int num, String start, int length) {
        String temp = "" + num;
        int len = length - temp.length();
        for (int i = 0; i < len; i++) {
            temp = "0" + temp;
        }
        start += temp;
        return start;
    }

}
